package com.muyi.base.service.impl;

import com.muyi.base.dto.UserReq;
import com.muyi.base.model.User;
import com.muyi.base.repository.UserRepository;
import com.muyi.base.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * @Author: Administrator
 * @Date: 2018/11/20 20 14
 * @Description:
 */
@Service
@CacheConfig(cacheNames = "User")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void save(UserReq userReq) {
        User user = new User();
        user.setName(userReq.getName());
        user.setPasswd(userReq.getPasswd());
        userRepository.save(user);
    }

    @Override
    public void saveRedis(UserReq userReq) {
        ValueOperations<String,User> vo = redisTemplate.opsForValue();
        User user = new User();
        user.setName(userReq.getName());
        user.setPasswd(userReq.getPasswd());
        vo.set(user.getName(), user);
    }

    @Override
    @Cacheable()
    public User findByName(String name) {
        User user = userRepository.findByName(name);
        return user;
    }
}
